package com.java2007.zhangyu.restaurant.dao.impl;

import com.java2007.zhangyu.restaurant.dao.OrderDetailDao;
import com.java2007.zhangyu.restaurant.pojo.OrderDetail;
import com.java2007.zhangyu.restaurant.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author zy
 * @Version
 * @date 2021/01/29
 */
public class OrderDetailDaoImpl implements OrderDetailDao {
    /**
     * 保存订单详情
     * @param orderDetail
     * @return
     * @throws SQLException
     */
    @Override
    public int save(OrderDetail orderDetail) throws SQLException {
        QueryRunner runner = new QueryRunner();
        String sql = "INSERT INTO `t_order_detail` " +
                " (`od_id`,`f_id`,`f_name`,`f_price`,`num`,`total_price`,`o_create_time`,`o_update_time`, o_id) " +
                " VALUES (?,?,?,?,?,?,?,?,?)";
        return runner.update(JdbcUtils.getConnection(), sql, orderDetail.getOdId(), orderDetail.getfId(),
                orderDetail.getfName(), orderDetail.getfPrice(), orderDetail.getNum(),
                orderDetail.getTotalPrice(), orderDetail.getoCreateTime(),
                orderDetail.getoUpdateTime(), orderDetail.getoId());
    }
    /**
     * 订单详情查询
     * @param
     * @return
     * @throws SQLException
     */
    @Override
    public List<OrderDetail> queryByOrderId(Long oId) throws SQLException {
        QueryRunner runner = new QueryRunner();
        // o_id
        String sql = "SELECT `od_id` odId,`f_id` fId,`f_name` fName,`f_price` fPrice,`num`,\n" +
                "`total_price` totalPrice,`o_create_time` createTime,`o_update_time` updateTime, o_id oId\n" +
                " FROM t_order_detail WHERE o_id = ?";
        return runner.query(JdbcUtils.getConnection(), sql, new BeanListHandler<>(OrderDetail.class),oId);
    }
}
